package com.bit.cfdi.web.validators.perfil;

import org.zkoss.bind.ValidationContext;
import org.zkoss.bind.validator.AbstractValidator;

public class RepeatNewPasswordValidator extends AbstractValidator {
	
	private String fieldName;
	private String passwordToCompare;
	
	public RepeatNewPasswordValidator(final String fieldName) {
		this.fieldName = fieldName;
	}

	@Override
	public void validate(final ValidationContext ctx) {
		String password = (String) ctx.getProperties(fieldName)[0].getValue();
		if(passwordToCompare == null || passwordToCompare.trim().isEmpty()) {
			return;
		}
		if(password == null || password.trim().isEmpty()) {
			addInvalidMessage(ctx, fieldName, "Los passwords no coinciden");
			return;
		}
		if(!passwordToCompare.equals(password)) {
			addInvalidMessage(ctx, fieldName, "Los passwords no coinciden");
			return;
		}
	}
	
	public String getPasswordToCompare() {
		return passwordToCompare;
	}
	
	public void setPasswordToCompare(final String passwordToCompare) {
		this.passwordToCompare = passwordToCompare;
	}

}
